home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / java / threads / example-1.1 / RaceApplet1_1.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.0 KB  |  95 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.awt.event.MouseAdapter;
  19. import java.awt.event.MouseEvent;
  20.  
  21. public class RaceApplet1_1 extends java.applet.Applet implements Runnable {
  22.  
  23.     final static int NUMRUNNERS = 2;
  24.     final static int SPACING = 20;
  25.  
  26.     Runner[] runners = new Runner[NUMRUNNERS];
  27.  
  28.     Thread updateThread = null;
  29.  
  30.     public void init() {
  31.         String raceType = getParameter("type");
  32.         for (int i = 0; i < NUMRUNNERS; i++) {
  33.             runners[i] = new Runner();
  34.             if (raceType.compareTo("unfair") == 0)
  35.                     runners[i].setPriority(i+2);
  36.             else
  37.                     runners[i].setPriority(2);
  38.         }
  39.         if (updateThread == null) {
  40.             updateThread = new Thread(this, "Thread Race");
  41.             updateThread.setPriority(NUMRUNNERS+2);
  42.         }
  43.         addMouseListener(new MyAdapter());
  44.     }
  45.  
  46.     class MyAdapter extends MouseAdapter {
  47.         public void mouseClicked(MouseEvent evt) {
  48.             if (!updateThread.isAlive())
  49.                 updateThread.start();
  50.             for (int i = 0; i < NUMRUNNERS; i++) {
  51.                 if (!runners[i].isAlive())
  52.                     runners[i].start();
  53.             }
  54.         }
  55.     }
  56.  
  57.     public void paint(Graphics g) {
  58.         g.setColor(Color.lightGray);
  59.         g.fillRect(0, 0, getSize().width, getSize().height);
  60.         g.setColor(Color.black);
  61.         for (int i = 0; i < NUMRUNNERS; i++) {
  62.             int pri = runners[i].getPriority();
  63.             g.drawString(new Integer(pri).toString(), 0, (i+1)*SPACING);
  64.         }
  65.         update(g);
  66.     }
  67.  
  68.     public void update(Graphics g) {
  69.         for (int i = 0; i < NUMRUNNERS; i++) {
  70.             g.drawLine(SPACING, (i+1)*SPACING, SPACING + (runners[i].tick)/1000, (i+1)*SPACING);
  71.         }
  72.     }
  73.  
  74.     public void run() {
  75.         while (Thread.currentThread() == updateThread) {
  76.             repaint();
  77.             try {
  78.                 Thread.sleep(10);
  79.             } catch (InterruptedException e) {
  80.             }
  81.         }
  82.     }    
  83.  
  84.     public void stop() {
  85.         for (int i = 0; i < NUMRUNNERS; i++) {
  86.             if (runners[i].isAlive()) {
  87.                 runners[i] = null;
  88.             }
  89.         }
  90.         if (updateThread.isAlive()) {
  91.             updateThread = null;
  92.         }
  93.     }
  94. }
  95.